package com.liuweidihuangwan.jingdong.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liuweidihuangwan.jingdong.exception.BusinessException;
import com.liuweidihuangwan.jingdong.exception.ErrorCode;
import com.liuweidihuangwan.jingdong.model.dto.shangpin.ShangpinQueryRequest;
import com.liuweidihuangwan.jingdong.model.entity.Image;
import com.liuweidihuangwan.jingdong.model.vo.ShangpinVO;
import com.liuweidihuangwan.jingdong.service.ImageService;
import com.liuweidihuangwan.jingdong.service.ShangpinService;
import com.liuweidihuangwan.jingdong.model.entity.Shangpin;
import com.liuweidihuangwan.jingdong.mapper.ShangpinMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author Administrator
* @description 针对表【shangpin】的数据库操作Service实现
* @createDate 2025-01-08 11:48:17
*/
@Service
public class ShangpinServiceImpl extends ServiceImpl<ShangpinMapper, Shangpin>
    implements ShangpinService {

    @Resource
    ImageService imageService;

    /**
     * 查询商品
     * @param shangpinQueryRequest
     * @return
     */
    @Override
    public List<ShangpinVO> queryShangpinList(ShangpinQueryRequest shangpinQueryRequest) {

        QueryWrapper<Shangpin> queryWrapper = getQueryWrapper(shangpinQueryRequest);
        List<Shangpin> list = list(queryWrapper);
        List<ShangpinVO> shangpinVOS = new ArrayList<>();
        for (Shangpin shangpin : list) {
            shangpinVOS.add(getShangpinVO(shangpin));
        }
        //向商品中添加图片
        List<Integer> ids = shangpinVOS.stream().map(ShangpinVO::getSpID).collect(Collectors.toList());
        if(CollUtil.isEmpty(ids)){
            return new ArrayList<>();
        }
        Map<Integer, List<Image>> map = imageService.lambdaQuery().in(Image::getKindID,ids).list().stream().collect(Collectors.groupingBy(Image::getKindID));
        for (ShangpinVO shangpinVO : shangpinVOS) {
            Integer spID = shangpinVO.getSpID();
            List<Image> images = map.get(spID);
            shangpinVO.setImages(images);
        }
        return shangpinVOS;
    }

    /**
     * 将商品转换为商品VO类
     * @param shangpin
     * @return
     */
    ShangpinVO getShangpinVO(Shangpin shangpin){
        if(shangpin == null){
            return null;
        }
        ShangpinVO shangpinVO = new ShangpinVO();
        BeanUtils.copyProperties(shangpin,shangpinVO);
        return shangpinVO;
    }

    /**
     * 获得商品查询的Wrapper
     * @return
     */
    QueryWrapper<Shangpin> getQueryWrapper(ShangpinQueryRequest shangpinQueryRequest){
        if(shangpinQueryRequest == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"传递参数为空");
        }
        QueryWrapper<Shangpin> queryWrapper = new QueryWrapper<>();
        Integer spID = shangpinQueryRequest.getSpID();
        String searchText = shangpinQueryRequest.getSearchText();
        BigDecimal maxPrice = shangpinQueryRequest.getMaxPrice();
        BigDecimal minPrice = shangpinQueryRequest.getMinPrice();
        Integer salesNum = shangpinQueryRequest.getSalesNum();
        Integer repertory = shangpinQueryRequest.getRepertory();
        Integer labelID = shangpinQueryRequest.getLabelID();

        queryWrapper.eq(spID != null,"spID",spID);

        queryWrapper.le(maxPrice != null,"maxPrice",maxPrice);
        queryWrapper.ge(minPrice != null,"minPrice",minPrice);
        queryWrapper.eq(salesNum != null,"saleNum",salesNum);
        queryWrapper.eq(repertory != null,"repertory",repertory);
        queryWrapper.eq(labelID != null,"labelID",labelID);
        if(StringUtils.isNotBlank(searchText)){
            queryWrapper.and(qw ->{
                qw.like("name",searchText)
                        .or()
                        .like("introduce",searchText);
            });
        }
        return queryWrapper;
    }
}




